import { Injectable, Logger } from '@nestjs/common';
import { ArticleCategoryEntity } from '~/modules/article/article-category/article-category.entity';
import { ArticleCategoryService } from '~/modules/article/article-category/article-category.service';
import { ArticleContentCollectService } from '~/modules/article/article-content/article-content-collect/article-content-collect.service';
import { ArticleContentLikeService } from '~/modules/article/article-content/article-content-like/article-content-like.service';
import { ArticleContentEntity } from '~/modules/article/article-content/article-content.entity';
import { ArticleContentService } from '~/modules/article/article-content/article-content.service';
import { ArticleContentDetailEntity } from './miniapp-article.model';
import { PagerDto } from '~/common/dto/pager.dto';
import { Pagination } from '~/helper/paginate/pagination';
import { isNil } from 'lodash';
import { ArticleContentShareService } from '~/modules/article/article-content/article-content-share/article-content-share.service';

@Injectable()
export class MiniappArticleService {
  private readonly logger = new Logger(MiniappArticleService.name);

  constructor(
    private readonly articleContentService: ArticleContentService,
    private readonly articleCategoryService: ArticleCategoryService,
    private readonly articleContentLikeService: ArticleContentLikeService,
    private readonly articleContentCollectService: ArticleContentCollectService,
    private readonly articleContentShareService: ArticleContentShareService
  ) {}

  async findRecommend(tenant: IAuthTenant): Promise<ArticleContentEntity[]> {
    return await this.articleContentService.findRecommend(tenant?.id ?? null);
  }

  /**
   * 获取文章详情
   */
  async findOne(id: number, user: IAuthUser): Promise<Partial<ArticleContentDetailEntity>> {
    const article: Partial<ArticleContentDetailEntity> = await this.articleContentService.findOne(
      id,
      user?.uid ?? null
    );

    // 如果有视频，分割成数组
    if (article?.video) {
      article.videos = article.video.split(',');
    }

    // 查询数据 hasLiked, hasCollected
    article.hasLiked = await this.articleContentLikeService.hasLiked(id, user?.uid ?? null);
    article.hasCollected = await this.articleContentCollectService.hasCollected(
      id,
      user?.uid ?? null
    );

    return article;
  }

  async findAllByTenantId(tenant: IAuthTenant): Promise<ArticleCategoryEntity[]> {
    return await this.articleCategoryService.findAllByTenantId(tenant?.id ?? null);
  }

  async findListByCategoryId(
    dto: PagerDto & { categoryId: number; tenantId: number }
  ): Promise<Pagination<ArticleContentEntity>> {
    return await this.articleContentService.findListByCategoryId(dto);
  }

  async findByName(name: string, tenantId: number): Promise<ArticleContentEntity[]> {
    return await this.articleContentService.findByName(name, tenantId);
  }

  async like(contentId: number, user: IAuthUser): Promise<void> {
    await this.articleContentLikeService.create(contentId, user.uid);

    await this.articleContentService.addLikeCount(contentId);
  }

  async unlike(contentId: number, user: IAuthUser): Promise<void> {
    await this.articleContentLikeService.delete(contentId, user.uid);

    await this.articleContentService.deleteLikeCount(contentId);
  }

  async collect(contentId: number, user: IAuthUser): Promise<void> {
    await this.articleContentCollectService.create(contentId, user.uid);

    await this.articleContentService.addCollectCount(contentId);
  }

  async uncollect(contentId: number, user: IAuthUser): Promise<void> {
    await this.articleContentCollectService.delete(contentId, user.uid);

    await this.articleContentService.deleteCollectCount(contentId);
  }

  /**
   * 分享文章
   * @param contentId 文章ID
   * @param userId 用户ID
   */
  async share(contentId: number, userId: number): Promise<void> {
    await this.articleContentService.addShareCount(contentId);

    if (isNil(userId)) {
      this.logger.error(`分享文章时，用户ID不能为空， 文章ID: ${contentId}`);
    }

    await this.articleContentShareService.create(contentId, userId);
  }

  async getCollectionByUser(userId: number): Promise<Array<Partial<ArticleContentEntity>>> {
    return await this.articleContentCollectService.getCollectionByUser(userId);
  }
}
